const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp()
Page({
  data: {
    bestGoods: [],
    keywords:'',
    searchtype:['智能影院','神画大威','支架','幕布','眼镜'],
    index:"6"
  },
  onShareAppMessage: function () {
    return {
      title: '神画官网微信小程序商城',
      desc: '神画官网微信小程序商城',
      path: '/pages/index/index'
    }
  },
  searchData:function(e){
    let that=this;
    let id=e.currentTarget.dataset.indexId;
    let searchtype = that.data.searchtype;
    let keywords=searchtype[id];
    that.setData({
      index:id
    });
    that.getsearch(keywords);
  },
  bindsearchData:function(e){
    let that = this;
    let keywords=e.detail.value;
    if(keywords==""){
      return false;
    }
    that.getsearch(keywords);
  },
  getsearch(keywords){
    let that = this;
    util.request(api.searchData, {keywords: keywords }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          bestGoods: res.data
        });
      } else {
        wx.showToast({
          title: res.data,
          image: "/static/images/icon_error.png",
          icon: 'success',
          duration: 2000
        })
      }
    });
  },
  clearwd:function(){
    this.setData({
      keywords:''
    });
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: "搜索商品"
    })
    this.setData({
      index:"6"
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
})
